﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Infragistics.Win.UltraWinGrid;
using Pasta.Medio.Models;
using Infragistics.Win.UltraWinStatusBar;

namespace Pasta.Medio
{
	public partial class Medio : Form, IMedio
	{
		private Settings _settings;
		private MediaImportModel _model;

		private List<Media> _media;
		private string[] _args;

		public Medio(string[] args)
		{
			_args = args;
			InitializeComponent();

			// service injection buildup
			Services.Settings = new Settings("Pasta", "Medio");
			Services.Model = new MediaImportModel( this );

			// dependencies
			_settings = Services.Settings;
			_model = Services.Model;
		}

		/// <summary>
		/// Return the selected media type
		/// </summary>
		public Enums.MediaType MediaType
		{
			get
			{
				Enums.MediaType type = Enums.MediaType.Photo;
				switch (optFilter.CheckedIndex)
				{
					case 0:
						type = Enums.MediaType.Photo;
						break;
					case 1:
						type = Enums.MediaType.Video;
						break;
				}
				return type;
			}
		}

		private void MedioForm_Load(object sender, EventArgs e)
		{
			// populate the UI controls
			optFilter.CheckedIndex = 0;
			teRenamePattern.Text = _settings.RenamePattern;

			statusBar.Panels[TextPanel].SetMessagePanel();

			// bind the media collection to the grid
			_media = new List<Media>();
			mediaGrid.DataSource = _media;

			if (_args.Length > 0 && Directory.Exists(_args[0]))
			{
				lblFolder.Text = _args[0];
			}
		}

		private void CreateTargetNames()
		{
			var folder = lblFolder.Text;
			if (!String.IsNullOrWhiteSpace(folder) && Directory.Exists(folder))
			{
				_media.Clear();
				_media.AddRange(_model.ListMedia(folder, MediaType, teRenamePattern.Text));

				mediaGrid.Rows.Refresh(RefreshRow.RefreshDisplay);
				mediaGrid.DisplayLayout.Bands[0].Columns[2].PerformAutoResize(PerformAutoSizeType.AllRowsInBand);
				mediaGrid.DisplayLayout.Bands[0].Columns[3].PerformAutoResize(PerformAutoSizeType.AllRowsInBand);
			}
		}

		private void mediaGrid_InitializeLayout(object sender, InitializeLayoutEventArgs e)
		{
			mediaGrid.DisplayLayout.Override.CellClickAction = CellClickAction.RowSelect;
			mediaGrid.DisplayLayout.Bands[0].Columns[0].CellClickAction = CellClickAction.EditAndSelectText;
			mediaGrid.DisplayLayout.Bands[0].Columns[2].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.DateTimeWithoutDropDown;
			mediaGrid.DisplayLayout.Bands[0].Columns[3].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.DateTimeWithoutDropDown;
		}

		private void btnRename_Click(object sender, EventArgs e)
		{
			_model.CopyRenameMedia(_media);
			CreateTargetNames();
		}

		private void Refresh(object sender, EventArgs e)
		{
			CreateTargetNames();
		}

		private void lblFolder_Click(object sender, EventArgs e)
		{
			if (folderBrowser.ShowDialog() == DialogResult.OK)
			{
				lblFolder.Text = folderBrowser.SelectedPath;
			}
		}

		private void ultraGrid2_InitializeRow(object sender, InitializeRowEventArgs e)
		{
			Media media = e.Row.ListObject as Media;
			if (media != null)
			{
				if (media.Shot.HasValue && media.Shot.Value < media.Modified)
					e.Row.Cells["Modified"].Appearance.ForeColor = Color.Gray;
				else if (media.Shot.HasValue && media.Shot.Value > media.Modified)
					e.Row.Cells["Shot"].Appearance.ForeColor = Color.Gray;
			}
		}

		private const string TextPanel = "StatusPanel";
		private const string ProgressLabelPanel = "ProgressLabel";
		private const string ProgressPanel = "Progress";

		public void UpdateProgress(int percentage)
		{
			statusBar.Panels[ProgressLabelPanel].Visible = (percentage >= 0);
			statusBar.Panels[ProgressPanel].ProgressBarInfo.Value = percentage;
		}

		public void UpdateStatus(string message, int sec10 = 0)
		{
			statusBar.Panels[TextPanel].ShowMessage(message, sec10);
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			UpdateStatus(DateTime.Now.ToString(), 30);
		}
	}

	public static class ControlExtensions
	{
		private static UltraStatusPanel _panel = null;
		private static string _oldLabel = null;
		private static Timer _statusTimer = new Timer();

		public static void SetMessagePanel(this UltraStatusPanel panel)
		{
			_panel = panel;
			_statusTimer.Tick += new EventHandler(_statusTimer_Tick);
			_statusTimer.Interval = 100;
			_statusTimer.Start();
			_statusTimer.Enabled = false;
		}

		public static void ShowMessage( this UltraStatusPanel panel, string message, int sec10)
		{
			if (_panel != null)
			{
				if (sec10 > 0 && !_statusTimer.Enabled)
				{
					_oldLabel = _panel.Text;
				}
				_statusTimer.Stop();
				_panel.Text = message;
				_statusTimer.Interval = 100 * sec10;
				_statusTimer.Enabled = true;
			}
		}

		private static void _statusTimer_Tick(object sender, EventArgs e)
		{
			_panel.Text = _oldLabel;
			_statusTimer.Enabled = false;
		}
	}
}
